package com.service;

import com.WxOpenIdVo;
import com.alibaba.fastjson.JSONObject;
import com.utils.WeiXinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@SuppressWarnings("ALL")
@Service
public class WxLoginService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.secret}")
    private String secret;

    /**
     * 获取用户openid
     * @param code
     * @return
     */

    public String  wxLogin(String code,String nick,String head,String miwen,String iv){
        String url="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        System.out.println("url=====:"+url);
        String newUrl=String.format(url,appid,secret,code);
        System.out.println("newUrl====:"+newUrl);
        String resultString=restTemplate.getForObject(newUrl,String.class);
        WxOpenIdVo vo= JSONObject.parseObject(resultString,WxOpenIdVo.class);
        System.out.println("vo=====:"+vo);

        //解密加密的用户信息
        String sessionKey=vo.getSession_key();
        String jiemi= WeiXinUtil.decryptData(miwen,sessionKey,iv);
        System.out.println("jiemi:"+jiemi);



        System.out.println("code=======:"+code);
        System.out.println("nick======:"+nick);
        System.out.println("head======:"+head);
        System.out.println("vo.getOpenid()=======:"+vo.getOpenid());

        return "";
    }



}
